package com.aaa.system.service.impl;

import com.aaa.common.entity.AclUser;
import com.aaa.common.util.JWTUtil;
import com.aaa.common.vo.Result;
import com.aaa.security.utils.WebUtils;
import com.aaa.system.dao.AclUserDao;
import com.aaa.system.service.AclUserService;
import com.aaa.system.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author ：Mickey
 * @date ：Created in 2022/12/1 20:29
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class AclUserServiceImpl implements AclUserService {
    @Autowired
    private AclUserDao userDao;
    @Override
    public Result findByName(String username) {
        QueryWrapper<AclUser> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("is_deleted",0);
        AclUser aclUser = userDao.selectOne(wrapper);
        if(aclUser!=null){
            return new Result<AclUser>(2000,"查询用户信息成功",aclUser);
        }
        return null;
    }
    @Override
    public Result<AclUser> findUserinfo() {
        //1.得到当前用户的名称
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> map = JWTUtil.getTokenChaim(token);
        String username = (String) map.get("username");

        QueryWrapper<AclUser> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("is_deleted",0);
        AclUser aclUser = userDao.selectOne(wrapper);

        return new Result<>(2000,"获取用户信息",aclUser);
    }

    //如果使用mp分页 需要配置分页拦截器
    @Override
    public Result<IPage<AclUser>> findByConditionPage(Integer current, Integer pageSize, UserVo userVo) {
        IPage<AclUser> page=new Page(current,pageSize);

        QueryWrapper<AclUser> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(userVo.getUsername())){
            wrapper.like("username",userVo.getUsername());
        }

        if(StringUtils.hasText(userVo.getStartDate())){
            wrapper.ge("gmt_create",userVo.getStartDate());
        }

        if(StringUtils.hasText(userVo.getEndDate())){
            wrapper.le("gmt_create",userVo.getEndDate());
        }

        IPage<AclUser> iPage = userDao.selectPage(page, wrapper);


        return new Result<>(2000,"查询成功",iPage);
    }

}
